/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.ubc.icapture.genapha.actions;

/**
 *
 * @author btripp
 */
import ca.ubc.icapture.genapha.beans.Login;
import ca.ubc.icapture.genapha.forms.AllListForm;
import ca.ubc.icapture.genapha.forms.GeneSnpForm;
import icapture.beans.DB.Gene;
import icapture.beans.DB.KeggPathway;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * @version 	1.0
 * @author
 */
public class GenePathwayExportAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        //Now set and send the output.
        response.reset();
        String filename = "";
        String delimiter = "";
        response.addHeader("cache-control", "must-revalidate");
        response.setHeader("Content-type", "application/xls");
        GeneSnpForm gsForm = (GeneSnpForm) form;
        delimiter = gsForm.getDlimiter();
        filename = gsForm.getFilename();
        HashMap<KeggPathway, TreeSet<Gene>> mapEntry = gsForm.getPathwayGeneMap();
        Set<Gene> geneList = gsForm.getGeneList();
        PrintWriter pw = response.getWriter();
        if (delimiter.equals("tab")) {
            delimiter = "\t";
            response.setHeader("Content-disposition", "attachment; filename=" + filename + ".txt");
            formatOutput(delimiter, pw, mapEntry, geneList);
        } else if (delimiter.equals("csv")) {
            delimiter = ",";
            response.setHeader("Content-disposition", "attachment; filename=" + filename + ".csv");
            formatOutput(delimiter, pw, mapEntry, geneList);
        }
        //Now output the results and return null;
        pw.flush();
        response.flushBuffer();
        return mapping.findForward("snpGeneExcel");
    }

    private void formatOutput(String delimiter, PrintWriter pw, HashMap<KeggPathway, TreeSet<Gene>> mapEntry, Set<Gene> geneList) {
        String excelOutput = "";
        String strItr = "";
        Set<KeggPathway> keys = mapEntry.keySet();

        String header = "Pathway ID" + delimiter + "Name" + delimiter;
        Iterator<Gene> geneListItr = geneList.iterator();
        while (geneListItr.hasNext()){
            header += geneListItr.next().getName();
            if (geneListItr.hasNext()){
                header += delimiter;
            }
        }
        header += "\r\n";
        pw.print(header);
        for (KeggPathway itr : keys) {
            excelOutput = "";
            String pathwayID = itr.getPathwayID();
            String pathwayName = itr.getName();
            TreeSet<Gene> tmp = mapEntry.get(itr);
            excelOutput += pathwayID + delimiter + pathwayName + delimiter;
            //list of genes submitted
            check:for (Gene geneListFor : geneList) {
                //list of genes found in this pathway
                for (Gene treeItr : tmp) {
                    if (geneListFor.equals(treeItr)) {
                        excelOutput += "X" + delimiter;
                        continue check;
                    }
                }
                excelOutput += "" + delimiter;

            }
            excelOutput += "\r\n";
            //print
            pw.print(excelOutput);
            //clear
            excelOutput = "";
        }
        pw.flush();
    }
}
